﻿using UnityEngine;
using UnityEditor;

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;

namespace DashFire.Assetbundle
{
  [System.Serializable]
  public class ExportConfigWin : EditorWindow
  {
    private static Vector2 m_WinMinSize = new Vector2(600.0f, 600.0f);
    private static Rect m_WinPosition = new Rect(300.0f, 200.0f, m_WinMinSize.x, m_WinMinSize.y);
    private static ExportConfigWin m_MainWin = null;

    bool IsBuidDirConfigs = false;
    List<bool> IsBuildDirConfig_FoldList = new List<bool>();

    private GUIContent
      insertContent = new GUIContent("+", "duplicate this point"),
      deleteContent = new GUIContent("-", "delete this point");
    private static GUILayoutOption
      buttonWidth = GUILayout.MaxWidth(20f);

    private void Initialize()
    {
      bool ret = ExportConfig.Load();
      if (!ret) {
        EditorUtility.DisplayDialog(
          "Confirm",
          "LoadConfig Failed!",
          "OK");
      }
      IsBuildDirConfig_FoldList.AddRange(new bool[ExportConfig.s_ExportTargetList.Count]);
    }
    private void Apply()
    {
      bool ret = ExportConfig.Save();
      if (ret) {
        EditorUtility.DisplayDialog(
          "Confirm",
          "SaveConfig Success!",
          "OK");
      } else {
        EditorUtility.DisplayDialog(
          "Confirm",
          "SaveConfig Failed!",
          "OK");
      }
    }
    private void OnEnable()
    {
      Initialize();
    }
    private void OnGUI()
    {
      EditorGUILayout.Space();
      ShowConfig();
      ConfirmOption();
      this.Repaint();
    }
    private void ShowConfig()
    {
      ExportConfig.s_Version = EditorGUILayout.TextField("Version:", ExportConfig.s_Version);
      EditorGUILayout.LabelField("Platform:", ExportConfig.GetPlatformName(ExportConfig.s_BuildOptionTarget));
      ExportConfig.s_GroupName = EditorGUILayout.TextField("GroupName:", ExportConfig.s_GroupName);
      ExportConfig.s_ResServerURL = EditorGUILayout.TextField("ResServerURL:", ExportConfig.s_ResServerURL);
      ExportConfig.s_BuildOptionTarget = (BuildTarget)EditorGUILayout.EnumPopup("BuildOptionTarget:", ExportConfig.s_BuildOptionTarget);

      IsBuidDirConfigs = EditorGUILayout.Foldout(IsBuidDirConfigs, "BuidDirConfigs");
      if (IsBuidDirConfigs) {

        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("ResBuildDirConfigsCount:", ExportConfig.s_ExportTargetCount.ToString());
        if (GUILayout.Button(insertContent, EditorStyles.miniButtonMid, buttonWidth)) {
          ExportConfig.s_ExportTargetCount++;
        }
        if (GUILayout.Button(deleteContent, EditorStyles.miniButtonRight, buttonWidth)) {
          ExportConfig.s_ExportTargetCount--;
        }
        EditorGUILayout.EndHorizontal();

        if (ExportConfig.s_ExportTargetCount >= 0) {
          while (true) {
            int countDiff = ExportConfig.s_ExportTargetCount - ExportConfig.s_ExportTargetList.Count;
            if (countDiff > 0) {
              ExportTargetInfo newConfig = new ExportTargetInfo();
              newConfig.m_TargetIndex = ExportConfig.s_ExportTargetList.Count + 1;
              ExportConfig.s_ExportTargetList.Add(newConfig);
              IsBuildDirConfig_FoldList.Add(true);
            } else if (countDiff < 0) {
              ExportConfig.s_ExportTargetList.RemoveAt(ExportConfig.s_ExportTargetList.Count - 1);
              IsBuildDirConfig_FoldList.RemoveAt(ExportConfig.s_ExportTargetList.Count - 1);
            } else {
              break;
            }
          }
          for (int index = 0; index < ExportConfig.s_ExportTargetCount; index++) {
            ExportTargetInfo config = ExportConfig.s_ExportTargetList[index];
            IsBuildDirConfig_FoldList[index] = EditorGUILayout.Foldout(IsBuildDirConfig_FoldList[index], index + ":" + config.m_TargetType);
            if (IsBuildDirConfig_FoldList[index]) {
              config.m_TargetType = EditorGUILayout.TextField(string.Format("TargetType{0}:", config.m_TargetIndex), config.m_TargetType);
              config.m_TargetDir = EditorGUILayout.TextField(string.Format("TargetDir{0}:", config.m_TargetIndex), config.m_TargetDir);
              config.m_TargetParrern = EditorGUILayout.TextField(string.Format("TargetParrern{0}:", config.m_TargetIndex), config.m_TargetParrern);
            }
          }
        }
      }
      EditorGUILayout.Space();
    }
    private void ConfirmOption()
    {
      EditorGUILayout.BeginHorizontal();
      if (GUILayout.Button("Apply", GUILayout.MaxWidth(80))) {
        Apply();
      }
      if (GUILayout.Button("Reset", GUILayout.MaxWidth(80))) {
        Initialize();
      }
      EditorGUILayout.EndHorizontal();
    }
    #region MenuItem
    [MenuItem("Export/Config", false, 1)]
    public static void ShowResConfigWin()
    {
      m_MainWin = EditorWindow.GetWindow<ExportConfigWin>("Config", false, typeof(ExportConfigWin));
      m_MainWin.position = m_WinPosition;
      m_MainWin.minSize = m_WinMinSize;
      m_MainWin.ShowTab();
      m_MainWin.Focus();
    }
    [MenuItem("Assets/Export Selected/Config", false, 1)]
    public static void Assets_ShowResConfigWin()
    {
      ShowResConfigWin();
    }
    #endregion
  }
}
